package com.cloud.order.converter;

import com.cloud.order.dto.OrderDTO;
import com.cloud.order.entity.OrderDetail;
import com.cloud.order.form.OrderForm;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class OrderConverter {

    public static OrderDTO OrderForm2OrderDTO(OrderForm orderForm) {
        Gson gson = new Gson();
        List<OrderDetail> list = new ArrayList<>();
        try {
            list = gson.fromJson(orderForm.getItems(), new TypeToken<List<OrderDetail>>() {
            }.getType());
        } catch (JsonSyntaxException e) {
            log.error("【json转换】错误,string={}",orderForm.getItems());
        }
        return OrderDTO.builder().buyerAddress(orderForm.getAddress())
                .buyerName(orderForm.getName()).buyerOpenid(orderForm.getOpenid())
                .buyerPhone(orderForm.getPhone()).list(list).build();
    }
}
